#!/bin/bash

WORK_DIR=$(cd $(dirname $0);pwd)

dnf_basic()
{
# 修改终端提示符
sed -i 's/W]/w]/' /etc/bashrc

# 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

# 关闭selinux
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config

# 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 优化内核参数
echo "fs.file-max = 1000000" >> /etc/sysctl.conf
echo "net.core.somaxconn = 32768" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies = 0" >> /etc/sysctl.conf
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf

# 开启路由转发
sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sysctl -p

# 修改资源使用上限参数
echo "
*           soft    nofile         65535
*           hard    nofile         65535
*           soft    stack          32768
*           hard    stack          32768
*           soft    nproc          65535
*           hard    nproc          65535
" >> /etc/security/limits.conf
}

dnf_source()
{
# 配置华为网络源
# dnf config-manager --add-repo=https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
# sed -i 's+download.docker.com+repo.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# sed -i 's+$releasever+9+' /etc/yum.repos.d/docker-ce.repo
dnf config-manager --add-repo=https://gitee.com/su-bushi/linux-script/raw/master/basic/docker-ce.repo
yum clean all && yum makecache

# 补充常用包
yum  install  -y  vim  tar  wget  telnet  zip  unzip  bash-completion  bind-utils  tree
}

dnf_docker()
{
# 安装docker-26.1.4
yum  install  -y  docker-ce-26.1.4  docker-ce-cli-26.1.4  containerd.io

# 导入 docker 配置
mkdir -p /etc/docker
cat <<EOF | tee /etc/docker/daemon.json
{
  "insecure-registries": ["10.10.104.160:5000","10.10.104.160:80","10.10.104.55:5000","10.10.104.55:80"],
  "registry-mirrors":
      ["https://l10nt4hq.mirror.aliyuncs.com",
       "https://registry.docker-cn.com",
       "https://docker.mirrors.ustc.edu.cn"
      ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {"max-size":"100m", "max-file":"3"},
  "storage-driver": "overlay2",
  "data-root": "/data/docker"
}
EOF

# 启动 docker 服务
systemctl daemon-reload
systemctl enable --now docker
docker -v
}

dnf_compose()
{
# 需上传docker-compose软件包到脚本路径
mv docker-compose-linux-x86_64 docker-compose
chmod +x docker-compose && mv ./docker-compose /usr/bin/
docker-compose -v
}

dnf_jdk()
{
# 需上传jdk软件包到脚本路径
sudo tar -zxf jdk-8u431-linux-x64.tar.gz -C /usr/local/

# 添加环境变量
sudo echo "
export JAVA_HOME=/usr/local/jdk1.8.0_431
export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
export PATH=\$PATH:\$JAVA_HOME/bin
" >> /etc/profile

# 刷新环境变量
source /etc/profile
java -version
}


dnf_basic
dnf_source
dnf_docker
dnf_compose
dnf_jdk